package leetcode.code1306;

import java.util.LinkedList;
import java.util.Queue;

public class Solution {
	public boolean canReach(int[] arr, int start) {
		int len = arr.length;
		boolean[] been = new boolean[len];
		been[start] = true;
		Queue<Integer> queue = new LinkedList<>();
		queue.add(start);
		while (!queue.isEmpty()) {
			int index = queue.poll();
			int num = arr[index];
			int t1 = index + num;
			int t2 = index - num;
			if (t1 >= 0 && t1 < len) {
				if (arr[t1] == 0) {
					return true;
				}
				if (!been[t1]) {
					been[t1] = true;
					queue.add(t1);
				}
			}

			if (t2 >= 0 && t2 < len) {
				if (arr[t2] == 0) {
					return true;
				}
				if (!been[t2]) {
					been[t2] = true;
					queue.add(t2);
				}
			}
		}
		return false;
	}
}
